<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>活跃用户</title>
    <meta name="keywords" content="H+后台主题,后台bootstrap框架,会员中心主题,后台HTML,响应式后台">
    <meta name="description" content="H+是一个完全响应式，基于Bootstrap3最新版本开发的扁平化主题，她采用了主流的左右两栏式布局，使用了Html5+CSS3等现代技术">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.7" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <!-- Data Tables -->
    <link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">

    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>基本 <small>分类，查找</small></h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="table_data_tables.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="table_data_tables.html#">选项1</a>
                                </li>
                                <li><a href="table_data_tables.html#">选项2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="">
                            <a data-toggle="modal" data-target="#myModal" href="javascript:void(0);" class="btn btn-primary ">添加行</a>
                        </div>
                        <table class="table table-striped table-bordered table-hover dataTables-example">
                            <thead>
                                <tr>
                                    <th>用户ID</th>
                                    <th>用户名</th>
                                    <th>实名</th>
                                    <th>性别</th>
                                    <th>生日</th>
                                    <th>手机号</th>
                                    <th>邮箱</th>
                                    <th>创建时间</th>
                                    <th>修改时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="listBody">
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>用户ID</th>
                                    <th>用户名</th>
                                    <th>实名</th>
                                    <th>性别</th>
                                    <th>生日</th>
                                    <th>手机号</th>
                                    <th>邮箱</th>
                                    <th>创建时间</th>
                                    <th>修改时间</th>
                                    <th>操作</th>
                                </tr>
                            </tfoot>
                        </table>
                        <!-- 模态框 -->
                        <div class="modal inmodal" id="myModal" tabindex="300" role="dialog" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content animated bounceInRight">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" onclick="clearModel()"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                                        </button>
                                        <h4 class="modal-title">用户表修改</h4>
                                        </div>
                                        <div class="modal-body">
                                            <form class="form-group" id="modal-form">
                                                <label>用户ID</label> <input type="number" name="id" readonly="readonly" class="form-control modalID">
                                                <label>用户名</label> <input type="text" name="username" placeholder="用户名" class="form-control modalName">
                                                <label>实名</label> <input type="email" name="realname" placeholder="实名" class="form-control modalRealName">
                                                <label>性别</label> 
                                                        <input type="radio" checked="" value=0 id="optionsRadios1" name="optionsRadios" >男</label>
                                                        <input type="radio" value=1 id="optionsRadios2" name="optionsRadios">女</label>
                                                        </br>
                                                        </br>
                                                <label>生日</label> <input type="date" name="birth" placeholder="生日" class="form-control modalBirth">
                                                <label>手机号</label> <input type="text" name="phone" placeholder="请输入您的手机号" class="form-control modalPhone">
                                                <label>邮箱</label> <input type="email" name="email" placeholder="请输入您的Email" class="form-control modalEmail">
                                            </form>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-white" data-dismiss="modal" onclick="clearModel()">关闭</button>
                                            <button type="button" class="btn btn-primary" onclick="submitModal(this)">保存</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 全局js -->
    <script src="../js/jquery.min.js?v=2.1.4"></script>
    <script src="../js/bootstrap.min.js?v=3.3.7"></script>

    <script src="../js/plugins/jeditable/jquery.jeditable.js"></script>

    <!-- Data Tables -->
    <script src="../js/plugins/dataTables/jquery.dataTables.js"></script>
    <script src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>

    <!-- 自定义js -->
    <script src="../js/content.js?v=1.0.0"></script>


    <!-- Page-Level Scripts -->
    <script>
        $(document).ready(function () {
            initTable()
        });
        function initTable(){
            var pageNum;
            var htmls='';
            $.ajax({
                url : "/user1/aliveList",
                success:function(data) {
                    $("#listBody").html('')
                    for (var index in data.data) {
                        let sex;
                        if(data.data[index].userSex==0){
                            sex="男";
                        }else if (data.data[index].userSex==1) {
                            sex="女";
                        }
                        htmls +=
                            '<tr className = "gradeX" class="grade">'+
                            '<td class = "id">' + data.data[index].userId + '</td>' +
                            '<td class="nickname">' + data.data[index].userAccount + '</td>' +
                            '<td class="realname">' + data.data[index].userName + '</td>' +
                            '<td class="center sex">' + sex + '</td>' +
                            '<td class="center birth">' + data.data[index].userBirth + '</td>'+
                            '<td class="phone">' + data.data[index].userPhone + '</td>' +
                            '<td class="email">' + data.data[index].userEmail + '</td>' +
                            '<td>' + data.data[index].createTime + '</td>' +
                            '<td class="center">' + data.data[index].updateTime+ '</td>' +
                            '<td class="center">' +
                            '<button class="btn btn-primary" id="update" data-toggle="modal" data-target="#myModal" data-backdrop="false" onClick="initModel(this)">修改</button>' +
                            '<button class="btn btn-primary" onClick="toDelete(this)">删除</button>' +
                            '<button class="btn btn-primary" onClick="toFroze(this)">冻结</button>' +
                            '</td>' +
                        '</tr>\n'
                    }
                    var body=document.getElementById('listBody');
                    body.innerHTML = htmls
                    $('.dataTables-example').dataTable();
                }
            })
        }
        //初始化模态框
        function initModel(obj){
            var tOj=$(obj).parent().parent();
            // console.log(mindex)
            // const tOj=$(".grade")[mindex]
            const id=parseInt( $(tOj).children(".id").text())
            console.log(id)
            $(".modalID").val(id);
            $(".modalName").val($(tOj).children(".nickname").text())
            $(".modalRealName").val($(tOj).children(".realname").text())
            const sex= $(tOj).children(".sex").text()
            if(sex=="男"){
                $("#optionsRadios1").prop("checked","checked")
            }else{
                $("#optionsRadios2").prop("checked","checked")
            }
            $(".modalBirth").val($(tOj).children(".birth").text())
            $(".modalPhone").val($(tOj).children(".phone").text())
            $(".modalEmail").val($(tOj).children(".email").text())
            var form=$("#modal-form")
            return form
        }
        function toDelete(obj) {
            parent.layer.open({
                type: 0,
                skin: 'layui-layer-demo', //样式类名
                closeBtn: 1, //不显示关闭按钮
                shift: 2,
                shadeClose: true, //开启遮罩关闭
                content: '确认删除？',
                yes:function () {
                    const tOj = $(obj).parent().parent();
                    const id = parseInt($(tOj).children(".id").text())
                    $.ajax({
                            url:"/user1/deleteUser",
                            type: "post",
                            data: {"id":id},
                            success: function () {
                                initTable();
                            }
                        }
                    )
                }
            });
        }
        function toFroze(obj) {
            const tOj = $(obj).parent().parent();
            const id = parseInt($(tOj).children(".id").text())
            $.ajax({
                    url:"/user1/frozeUser",
                    type: "post",
                    data: {"id":id},
                    success: function () {
                        initTable();
                    }
                }
            )
        }
        function clearModel(){
            $(".modalID").val('');
            $(".modalName").val('')
            $(".modalRealName").val('')
            $(".modalBirth").val('')
            $(".modalPhone").val('')
            $(".modalEmail").val('')
        }
        // 提交表单
        function submitModal(obj){
            var form=$("#modal-form")
            var data={
                id:$(".modalID").val(),
                nickname:$(".modalName").val(),
                realname:$(".modalRealName").val(),
                sex:$("input[name='optionsRadios']:checked").val(),
                birth:$(".modalBirth").val(),
                phone:$(".modalPhone").val(),
                email:$(".modalEmail").val()
            }
            if (data.id==null||data.id==''){
                addUser(data)
            }else {
                updateUser(data)
            }
        }
        function updateUser(data) {
            console.log("更新")
            $.ajax({
                url : "/user1/updateUser",
                type: "POST",
                data: JSON.stringify(data),
                processData: false,  // 不处理数据
                contentType: "application/json;charset=UFT-8",   // 不设置内容类型
                success:function(data){
                    initTable()
                }
            })
        }
        function addUser(data) {
            console.log("添加")
            $.ajax({
                url : "/user1/addUser",
                type: "POST",
                data: JSON.stringify(data),
                processData: false,  // 不处理数据
                contentType: "application/json;charset=UFT-8",   // 不设置内容类型
                success:function(data){
                    initTable()
                }
            })
        }
    </script>

<!--    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>-->
    <!--统计代码，可删除-->

</body>

</html>
